﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using WeChatSDK.Common;
using WeChatSDK.WeChatDto.Conversation.ReceivingMessage;
using WeChatSDK.WeChatDto.Conversation.ReceivingMessage.EventMessage;

namespace WeChatSDK.WeChatServiceComponent.Conversation.ReceivingMessage
{
    /// <summary>
    /// 接收消息服务
    /// </summary>
    public class ReceivingMessageService
    {
        /// <summary>
        /// 消息转换
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <exception cref="WeChatException"></exception>
        /// <returns></returns>
        public static ReceivingMessageDto MessageConvert(string message)
        {
            ReceivingMessageDto receivingMessageDto = WeChatXmlUtils.DeserializeToObject<ReceivingMessageDto>(message);
            switch (receivingMessageDto.MsgType)
            {
                case "text":
                    return WeChatXmlUtils.DeserializeToObject<TextMessageDto>(message);
                case "image":
                    return WeChatXmlUtils.DeserializeToObject<ImageMessageDto>(message);
                case "voice":
                    return WeChatXmlUtils.DeserializeToObject<VoiceMessageDto>(message);
                case "video":
                case "shortvideo":
                    return WeChatXmlUtils.DeserializeToObject<ShortVideoMessageDto>(message);
                case "location":
                    return WeChatXmlUtils.DeserializeToObject<LocationMessageDto>(message);
                case "link":
                    return WeChatXmlUtils.DeserializeToObject<LinkMessageDto>(message);
                case "event":
                    return TransfromEventDto(WeChatXmlUtils.DeserializeToObject<EventMessageDto>(message),message);
                default:
                    return receivingMessageDto;
            }
        }

        private static ReceivingMessageDto TransfromEventDto(EventMessageDto eventMessageDto,string message)
        {
            switch (eventMessageDto.Event)
            {
                case "wxa_widget_data":
                    return WeChatXmlUtils.DeserializeToObject<WidgetEventMessageDto>(message);
                case "LOCATION":
                    return WeChatXmlUtils.DeserializeToObject<LocationEventMessageDto>(message);
                case "subscribe":
                case "unsubscribe":
                    if (message.Contains("qrscene_"))
                    {
                        return WeChatXmlUtils.DeserializeToObject<QREventMessageDto>(message);
                    }
                    return WeChatXmlUtils.DeserializeToObject<SubscribeEventMessageDto>(message);
                case "SCAN":
                    return WeChatXmlUtils.DeserializeToObject<QREventMessageDto>(message);
                case "CLICK":
                    return WeChatXmlUtils.DeserializeToObject<ClickEventMessageDto>(message);
                case "VIEW":
                    return WeChatXmlUtils.DeserializeToObject<ViewEventMessageDto>(message);
                case "MASSSENDJOBFINISH":
                    return WeChatXmlUtils.DeserializeToObject<MPCallbackEventMessageDto>(message);
                case "scancode_push":
                case "scancode_waitmsg":
                    return WeChatXmlUtils.DeserializeToObject<ScanCodePushEventMessageDto>(message);
                case "pic_sysphoto":
                case "pic_photo_or_album":
                case "pic_weixin":
                    return WeChatXmlUtils.DeserializeToObject<PicSysphotoEventMessageDto>(message);
                case "location_select":
                    return WeChatXmlUtils.DeserializeToObject<LocationSelectEventMessageDto>(message);
                default:
                    return eventMessageDto;
            }
        }
    }
}
